﻿using System;
using System.Collections.Generic;
using System.Reflection;
using HarmonyLib;
using TaleWorlds.CampaignSystem;
using TaleWorlds.CampaignSystem.Actions;
using TaleWorlds.CampaignSystem.CharacterDevelopment;
using TaleWorlds.CampaignSystem.Conversation;
using TaleWorlds.CampaignSystem.Encounters;
using TaleWorlds.CampaignSystem.Extensions;
using TaleWorlds.CampaignSystem.MapEvents;
using TaleWorlds.CampaignSystem.Party;
using TaleWorlds.CampaignSystem.Settlements;
using TaleWorlds.Core;
using TaleWorlds.Library;
using TaleWorlds.Localization;

namespace MarriageFertility.Helpers
{

	internal class LogicArms
	{
		// Token: 0x0600035B RID: 859 RVA: 0x000136A0 File Offset: 0x000118A0
		public LogicArms()
		{
			
		}

		// Token: 0x0600035C RID: 860 RVA: 0x00013890 File Offset: 0x00011A90
		public static void InvokeNonPublicMethod(object instance, string methodName, object[] param)
		{
			Type type = instance.GetType();
			MethodInfo method = type.GetMethod(methodName, BindingFlags.Instance | BindingFlags.NonPublic);
			bool flag = method == null;
			if (flag)
			{
				method = type.BaseType.GetMethod(methodName, BindingFlags.Instance | BindingFlags.NonPublic);
			}
			method.Invoke(instance, param);
		}

		// Token: 0x0600035D RID: 861 RVA: 0x000138D4 File Offset: 0x00011AD4
		public static object InvokeNonPublicMethodWithReturn(object instance, string methodName, object[] param)
		{
			Type type = instance.GetType();
			MethodInfo method = type.GetMethod(methodName, BindingFlags.Instance | BindingFlags.NonPublic);
			bool flag = method == null;
			if (flag)
			{
				method = type.BaseType.GetMethod(methodName, BindingFlags.Instance | BindingFlags.NonPublic);
			}
			object result;
			try
			{
				result = method.Invoke(instance, param);
			}
			catch (TargetInvocationException ex)
			{
				throw ex.InnerException;
			}
			return result;
		}

		// Token: 0x0600035E RID: 862 RVA: 0x00013940 File Offset: 0x00011B40
		public static object SetNotPublicPro(object instance, string varName, object newVar)
		{
			Type type = instance.GetType();
			PropertyInfo property = type.GetProperty(varName, BindingFlags.Instance | BindingFlags.Public | BindingFlags.NonPublic);
			bool flag = property == null || property.SetMethod == null;
			if (flag)
			{
				property = type.BaseType.GetProperty(varName, BindingFlags.Instance | BindingFlags.Public | BindingFlags.NonPublic);
			}
			property.SetValue(instance, newVar);
			return property.GetValue(instance);
		}

		// Token: 0x0600035F RID: 863 RVA: 0x000139A0 File Offset: 0x00011BA0
		public static object GetNotPublicPro(object instance, string varName)
		{
			Type type = instance.GetType();
			PropertyInfo property = type.GetProperty(varName, BindingFlags.Instance | BindingFlags.Public | BindingFlags.NonPublic);
			bool flag = property == null;
			if (flag)
			{
				property = type.BaseType.GetProperty(varName, BindingFlags.Instance | BindingFlags.Public | BindingFlags.NonPublic);
			}
			return property.GetValue(instance);
		}

		// Token: 0x06000360 RID: 864 RVA: 0x000139E8 File Offset: 0x00011BE8
		public static object GetNotPublicVar(object instance, string varName)
		{
			Type type = instance.GetType();
			FieldInfo field = type.GetField(varName, BindingFlags.Instance | BindingFlags.Static | BindingFlags.NonPublic);
			bool flag = field == null;
			if (flag)
			{
				field = type.BaseType.GetField(varName, BindingFlags.Instance | BindingFlags.Static | BindingFlags.NonPublic);
			}
			return field.GetValue(instance);
		}

		// Token: 0x06000361 RID: 865 RVA: 0x00013A30 File Offset: 0x00011C30
		public static object SetNotPublicVar(object instance, string varName, object newVar)
		{
			Type type = instance.GetType();
			FieldInfo field = type.GetField(varName, BindingFlags.Instance | BindingFlags.Public | BindingFlags.NonPublic);
			bool flag = field == null;
			if (flag)
			{
				field = type.BaseType.GetField(varName, BindingFlags.Instance | BindingFlags.Public | BindingFlags.NonPublic);
			}
			field.SetValue(instance, newVar);
			return field.GetValue(instance);
		}
		

		// Token: 0x06000364 RID: 868 RVA: 0x00013D14 File Offset: 0x00011F14
		private void ChangeRelation(int relation, Hero hero)
		{
			bool flag = relation > 100;
			if (flag)
			{
				relation = 100;
			}
			bool flag2 = false;
			int num = relation - TaleWorlds.Library.MathF.Ceiling(hero.GetRelationWithPlayer());
			bool flag3 = TaleWorlds.Library.MathF.Ceiling(hero.GetRelationWithPlayer()) < 0 || TaleWorlds.Library.MathF.Ceiling(hero.GetRelationWithPlayer()) == 99;
			if (flag3)
			{
				num += 2;
				flag2 = true;
			}
			object[] array = new object[]
			{
				Hero.MainHero,
				hero,
				num,
				true
			};
			Hero hero2;
			Hero hero3;
			Campaign.Current.Models.DiplomacyModel.GetHeroesForEffectiveRelation(Hero.MainHero, hero, out hero2, out hero3);
			int value = CharacterRelationManager.GetHeroRelation(hero2, hero3) + num;
			value = MBMath.ClampInt(value, -100, 100);
			hero2.SetPersonalRelation(hero3, value);
			bool flag4 = flag2;
			if (flag4)
			{
				array[2] = (int)array[2] - 2;
			}
			CampaignEventDispatcher.Instance.OnHeroRelationChanged(hero2, hero3, num, true, ChangeRelationAction.ChangeRelationDetail.Default, Hero.MainHero, hero);
			MBTextManager.SetTextVariable("AD_RELATION_MAN_NAME", hero.Name, false);
			MBTextManager.SetTextVariable("AD_RELATION", relation);
			InformationManager.DisplayMessage(new InformationMessage(new TextObject("{=strAddRelationInfo}Change Relation with {AD_RELATION_MAN_NAME} to {AD_RELATION}", null).ToString()));
		}

		// Token: 0x06000365 RID: 869 RVA: 0x00013E44 File Offset: 0x00012044
		public void ArmDealerChangeRelation(int relation)
		{
			this.ChangeRelation(relation, Hero.OneToOneConversationHero);
		}

		// Token: 0x06000366 RID: 870 RVA: 0x00013E54 File Offset: 0x00012054
		public void ArmDealerChangeRelation(int relation, Hero hero)
		{
			this.ChangeRelation(relation, hero);
		}

		// Token: 0x06000367 RID: 871 RVA: 0x00013E60 File Offset: 0x00012060
		public bool IsConversationPlayerCompanion()
		{
			return Hero.OneToOneConversationHero.IsPlayerCompanion || Hero.OneToOneConversationHero == Hero.MainHero.Spouse;
		}

		// Token: 0x06000368 RID: 872 RVA: 0x00013E94 File Offset: 0x00012094
		public void ShowException(Exception ex, string extra)
		{
			string str = string.Empty;
			bool flag = ex.InnerException != null;
			if (flag)
			{
				str = ex.InnerException.Message;
			}
			else
			{
				str = ex.Message;
			}
			InformationManager.DisplayMessage(new InformationMessage(new TextObject(extra + str, null).ToString(), Colors.Red));
		}
		

		// Token: 0x04000152 RID: 338
		public static LogicArms instance;
		
		public bool hasKillPlan;

		// Token: 0x04000165 RID: 357
		public int duelCooldown = 168;

		// Token: 0x04000166 RID: 358
		public float bloodResume = 0.2f;

		// Token: 0x04000167 RID: 359
		public int buyFoodCooldown = 148;

		// Token: 0x04000168 RID: 360
		public List<MapEvent> mapEvents = new List<MapEvent>();

		// Token: 0x04000169 RID: 361
		public bool hasZhangSan;

		// Token: 0x0400016A RID: 362
		public bool hasYuenv;

		// Token: 0x0400016B RID: 363
		public Hero ZhangSanHero;

		// Token: 0x0400016C RID: 364
		public Hero YuenvHero;

		// Token: 0x0400016D RID: 365
		public CharacterObject character;

		// Token: 0x0400016E RID: 366
		public int battleState = -1;

		// Token: 0x0400016F RID: 367
		public int normalState = -1;

		// Token: 0x04000170 RID: 368
		public CampaignTime timeToPersude;

		// Token: 0x04000171 RID: 369
		public bool hasModifyDamage;

		// Token: 0x04000172 RID: 370
		public int arrowIndex = -1;

		// Token: 0x04000173 RID: 371
		public bool IsImmuseDamage;

		// Token: 0x04000174 RID: 372
		public int playerClanTier = -1;

		// Token: 0x04000175 RID: 373
		public bool isAtWarWithBlackFlag;

		// Token: 0x04000176 RID: 374
		public bool AllowBetray = true;

		// Token: 0x04000178 RID: 376
		public bool initBlackFlag;

	}
}
